package prolog.builtins
import prolog.terms._
import prolog.fluents._
import prolog.io._

final case class listing() extends FunBuiltin("listing", 1) {

  def listpred(p: Prog, pred: Term): Int = {
    val cs = p.db.matches(List(pred))
    if (cs.eq(null)) 0
    else {
      cs.foreach(TermParser.printclause)
      1
    }
  }

  override def exec(p: Prog) = {
    val pred = getArg(0)
    if (pred.isInstanceOf[Var]) {
      p.db.foreach(x =>
        { x._2.foreach(TermParser.printclause); println })
      1
    } else
      listpred(p, pred)
  }
}
